package cn.player.download.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.player.download.DownloadInfo;


public class DownloadInfoDao extends AbstractDao<DownloadInfo> {

    private static final String TABLE_NAME = DownloadInfo.class.getSimpleName();

    public DownloadInfoDao(Context context) {
        super(context);
    }

    public static void createTable(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + "(_id integer primary key autoincrement, " +
                "vid text, uid text, filename text, requestUrl text, targetFilePath text, " +
                "priority integer, fileSize long, progress float, downloadState integer)");
    }

    public static void dropTable(SQLiteDatabase db) {
        db.execSQL("drop table if exists " + TABLE_NAME);
    }

    public void insert(DownloadInfo info) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("insert into "
                        + TABLE_NAME
                        + "(vid, uid, filename, requestUrl, targetFilePath, priority, fileSize, progress, downloadState) values(?, ?, ?, ?, ?, ?, ?, ?, ?)",
                new Object[]{info.getVid(), info.getUid(), info.getFilename(),info.getRequestUrl(), info.getTargetFilePath(),
                        info.getPriority(), info.getFileSize(), info.getProgress(), info.getDownloadState()});
    }

    public void delete(String vid) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("delete from "
                        + TABLE_NAME
                        + " where vid = ?",
                new Object[]{vid});
    }

    public void update(String vid,int downloadState) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("update "
                        + TABLE_NAME
                        + " set downloadState = ?"
                        + " where vid = ? ",
                new Object[]{downloadState,vid});
    }

    public void update(String vid,int downloadState,String targetFilePath) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("update "
                        + TABLE_NAME
                        + " set downloadState = ? and targetFilePath = ?"
                        + " where vid = ? ",
                new Object[]{downloadState, targetFilePath, vid});
    }

    public void update(String vid, int progress, int downloadState) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("update "
                        + TABLE_NAME
                        + " set progress = ? and downloadState = ?"
                        + " where vid = ? ",
                new Object[]{progress, downloadState, vid});
    }

    public DownloadInfo find(String vid) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from "
                        + TABLE_NAME
                        + " where vid = ?",
                new String[]{vid});
        DownloadInfo info = null;
        while (cursor.moveToNext()) {
            info = new DownloadInfo();
            info.setVid(cursor.getString(cursor.getColumnIndex("vid")));
            info.setUid(cursor.getString(cursor.getColumnIndex("uid")));
            info.setFilename(cursor.getString(cursor.getColumnIndex("filename")));
            info.setTargetFilePath(cursor.getString(cursor.getColumnIndex("targetFilePath")));
            info.setRequestUrl(cursor.getString(cursor.getColumnIndex("requestUrl")));
            info.setPriority(cursor.getInt(cursor.getColumnIndex("priority")));
            info.setDownloadState(cursor.getInt(cursor.getColumnIndex("downloadState")));
            info.setProgress(cursor.getFloat(cursor.getColumnIndex("progress")));
            info.setFileSize(cursor.getLong(cursor.getColumnIndex("fileSize")));
            break;
        }
        cursor.close();
        return info;
    }


    public boolean exists(String vid) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from "
                        + TABLE_NAME
                        + " where vid = ?",
                new String[]{vid});
        boolean isExists = cursor.moveToNext();
        cursor.close();
        return isExists;
    }

}
